/**
 * 15/02/2014
 * 
 * PayRollController
 */
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.PayRollBOInterface;
import org.cuatrovientos.erp.model.PayRoll;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller class form PayRoll management
 * @author Javier Tejedor
 */
@Controller
public class PayRollController {

	//Get log4j logger
	private static final Logger logger = 
			Logger.getLogger(PayRollController.class);

	@Autowired
	private PayRollBOInterface payrollBO;

	/**
	 * default constructor
	 */
	public PayRollController () {
		logger.info("PayRoll BEAN instantiated");
	}

	/**
	 * handles default /payroll or /payroll/list request
	 * @return
	 */
	@RequestMapping({"/payroll","*","/payroll/list"})
	public ModelAndView payrollDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<PayRoll> payrollList = payrollBO.selectAll();
		model.put("payrolls", payrollList );
		
		return new ModelAndView("PayRollList", model);     
	}


	/**
	 * handles default /payroll/search request
	 * @param name
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/payroll/search"})
	public ModelAndView search(
			@RequestParam(value="name", defaultValue="", required=true) 
			String description) {
		
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<PayRoll> payrollList = payrollBO.search(description);
		model.put("payrolls", payrollList );
		
		return new ModelAndView("PayRollList", model);     
	}

	/**
	 * handles default /payroll/delete request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/payroll/delete"})
	public String deletePayRoll(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("delete " + id);

		payrollBO.deleteById(id);
		
		return "redirect:/payroll";
	}

	/**
	 * handles default /payroll/new request
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/payroll/new"})
	public ModelAndView newPayRoll() {
		logger.info("new payroll");
		
		PayRoll payroll = new PayRoll();

		return new ModelAndView("NewPayRoll", "payroll", payroll);
	}

	/**
	 * handles default /payroll/save request
	 * @param payroll
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/payroll/save"})
	public String newPayRoll(@Valid PayRoll payroll, BindingResult bindingResult) {
		logger.info("new payroll " + payroll.toString());

		//In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("payroll", payroll);
			
			return "NewPayRoll";
		}

		payrollBO.insert(payroll);
		
		return "redirect:/payroll";
	}

	/**
	 * handles default /payroll/detail request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/payroll/detail"})
	public ModelAndView payrollDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("Detail " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		PayRoll payroll = payrollBO.selectById(id);
		model.put("payroll", payroll);

		return new ModelAndView("PayRollDetail", model);
	}


	/**
	 * handles default /payroll/update request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/payroll/update"})
	public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("update " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		PayRoll payroll = payrollBO.selectById(id);
		model.put("payroll", payroll);
		
		return new ModelAndView("UpdatePayRoll",model);
	}


	/**
	 * handles default /payroll/saveupdate request
	 * @param payroll
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/payroll/saveupdate"})
	public String saveUpdate(@Valid PayRoll payroll, BindingResult bindingResult) {
		logger.info("update payroll " + payroll.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("payroll", payroll);
			
			return "UpdatePayRoll";
		}

		payrollBO.update(payroll);
		
		return "redirect:/payroll";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping({"/payroll/index"})
	public String sayHelloPage(Map<String, Object> model) {
		return "index/IndexIssuetype";
	}
	
}
